﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using HigLabo.WindowsAzure.RestService.Extensions;

namespace HigLabo.WindowsAzure.RestService.ServiceManagement
{
    public class Operation
    {
        public static readonly String ElementName = "Operation"; 
        
        public String ID { get; private set; }
        public OperationStatus Status { get; private set; }
        public HttpStatusCode? StatusCode { get; private set; }
        public String ErrorCode { get; private set; }
        public String ErrorMessage { get; private set; }

        public Operation(XElement xml)
        {
            if (xml == null)
            {
                throw new ArgumentNullException("xml");
            }
            this.ID = xml.ValueOrEmpty("ID");
            this.Status = xml.ValueToEnum("Status", OperationStatus.Failed);
            this.StatusCode = xml.ValueToEnum<HttpStatusCode>("HttpStatusCode");
            var err = xml.ElementByLocalName("Error");
            this.ErrorCode = err.ValueOrEmpty("Code");
            this.ErrorMessage = err.ValueOrEmpty("Message");
        }
    }
}
